﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Web.Mvc;
using System.Web.Routing;


namespace Shop.Framework.Services {
    public class RouteBuilder {

        private static object _Padlock = new object();
        private static RouteBuilder _Instance;

        public static RouteBuilder Instance {
            get {
                lock (_Padlock) {
                    if (_Instance == null)
                        _Instance = new RouteBuilder();

                    return _Instance;
                }
            }
        }

        public void BuildRoute( ) {
            RouteTable.Routes.Clear();

            #region Default
            //RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            //RouteTable.Routes.MapRoute(
            //    "Default",                                              // Route name
            //    "{controller}/{action}/{id}",                           // URL with parameters
            //    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            //);
            #endregion

            AddRoute("", new { controller = "Home", action = "Index" });

            AddRoute("login", new { controller = "Home", action = "Login", username = string.Empty, password = string.Empty });
            AddRoute("logout", new { controller = "Home", action = "Logout"});
            AddRoute("register.html", new { controller = "Home", action = "Register" });
            AddRoute("registration/save", new { controller = "Home", action = "SaveRegistration" });
            AddRoute("aboutus.html", new { controller = "Home", action = "About" });
            AddRoute("terms.html", new { controller = "Home", action = "Terms" });
            

            AddRoute("categories", new { controller = "Home", action = "GetCategories" });            
            AddRoute("designers", new { controller = "Home", action = "GetDesigners" });

            AddRoute("category", new { controller = "Home", action = "Category" });
            AddRoute("designer", new { controller = "Home", action = "Designer" });            

            /*Payment Routes*/
            AddRoute("checkout", new { controller = "Cart", action = "Checkout" });
            AddRoute("finish-payment.html", new { controller = "Cart", action = "Reciept" });            
            
            /*Payment Routes*/
            

            /*CMS Category*/
            AddRoute("cms/logout", new { controller = "CMS", action = "Logout" });
            AddRoute("cms/orders", new { controller = "CMS", action = "Orders" });
            AddRoute("cms/orders/{idCustomerOrder}.html", new { controller = "CMS", action = "OrderDetail", idCustomerOrder = 0 });
            AddRoute("cms/complete-order", new { controller = "CMS", action = "Completed" });
            AddRoute("cms", new { controller = "CMS", action = "CategoryList" });
            AddRoute("cms/login", new { controller = "CMS", action = "Login", login = "", password = "" });
            AddRoute("cms/listAction", new { controller = "CMS", action = "ListAction"});
            AddRoute("cms/{idCategory}/product-list/{page}", new { controller = "CMS", action = "ProductList", idCategory = 0, page = 1 });
            AddRoute("cms/{idCategory}/product-{idProduct}", new { controller = "CMS", action = "ProductDetail", idCategory = 0, idProduct = 0 });
            AddRoute("cms/category/idCategory", new {
                                  controller = "CMS", 
                                  action = "SelectCategory",
                                  idCategory = 0});
            AddRoute("cms/saveCategory", new { 
                controller = "CMS", 
                action = "SaveCategory"
            });
            /*CMS Category*/

            /*CMS Product*/
            AddRoute("cms/maintainance/product/save", new { controller = "CMS", action = "SaveProduct" });
            ///cms/maintainance/new-product/
            AddRoute("cms/maintainance/new-product/{idCategory}", new { controller = "CMS", action = "NewProduct", idCategory = 0 });

            AddRoute("cms/maintainance/get-images/{idProduct}", new { controller = "CMS", action = "GetProductImageList", idProduct = 0 });
            AddRoute("cms/maintainance/addProduct-image/{idProduct}", new { controller = "CMS", action = "AddImage", idProduct = 0 });
            AddRoute("cms/maintainance/modify-props/{idProduct}/{idProductImage}", new { controller = "CMS", action = "ModifyImage", idProduct = 0, idProductImage = 0 });

            /*CMS Product*/

            AddRoute("getCurrentContext", new { controller = "Home", action = "GetCurrentContext" });
            AddRoute("{categoryName}-{idCategory}.html", new {Controller = "Product", Action = "GetProductsByCategory", idCategory = 0, categoryName = string.Empty });
            AddRoute("{itemName}/{alias}/p/{page}.html", new { Controller = "Product", Action = "NavigatePage" });
            AddRoute("products/{name}/{idProduct}.html", new { Controller = "Product", Action = "GetProductItem" });
            AddRoute("designer/products/{designerName}/{idDesigner}.html", new { Controller = "Product", Action = "GetProductsByDesigner", idDesigner = 0, designerName = string.Empty });
            AddRoute("cart/add/{idProduct},{quantity}", new { Controller = "Cart", Action = "AddToCart", idProduct = 0 });
            AddRoute("cart/add", new { Controller = "Cart", Action = "AddToCart2", idProduct = 0 });
            AddRoute("cart/items.html", new { Controller = "Cart", Action = "Items"});
            AddRoute("cart/delete/{idProduct}", new { Controller = "Cart", Action = "Delete" , idProduct = 0, quantity = 1 });
            
        }

        private static void AddRoute(string url, object constraints) {
            RouteTable.Routes.Add(url, new Route(url, new MvcRouteHandler()) {
                Defaults = new RouteValueDictionary(constraints)
            });            
        }
    }
}
